﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.Achievements;
using Terraria.GameContent.UI.States;
using Terraria.GameInput;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.UI.Gamepad;

namespace Terraria.UI
{
	// Token: 0x020000E0 RID: 224
	public class IngameFancyUI
	{
		// Token: 0x06000B39 RID: 2873 RVA: 0x0000BD4A File Offset: 0x00009F4A
		public static void CoverNextFrame()
		{
			IngameFancyUI.CoverForOneUIFrame = true;
		}

		// Token: 0x06000B3A RID: 2874 RVA: 0x0000BD52 File Offset: 0x00009F52
		public static bool CanCover()
		{
			if (IngameFancyUI.CoverForOneUIFrame)
			{
				IngameFancyUI.CoverForOneUIFrame = false;
				return true;
			}
			return false;
		}

		// Token: 0x06000B3B RID: 2875 RVA: 0x0000BD64 File Offset: 0x00009F64
		public static void OpenAchievements()
		{
			IngameFancyUI.CoverNextFrame();
			Main.playerInventory = false;
			Main.editChest = false;
			Main.npcChatText = "";
			Main.inFancyUI = true;
			Main.InGameUI.SetState(Main.AchievementsMenu);
		}

		// Token: 0x06000B3C RID: 2876 RVA: 0x0000BD96 File Offset: 0x00009F96
		public static void OpenAchievementsAndGoto(Achievement achievement)
		{
			IngameFancyUI.OpenAchievements();
			Main.AchievementsMenu.GotoAchievement(achievement);
		}

		// Token: 0x06000B3D RID: 2877 RVA: 0x0000BDA8 File Offset: 0x00009FA8
		public static void OpenKeybinds()
		{
			IngameFancyUI.CoverNextFrame();
			Main.playerInventory = false;
			Main.editChest = false;
			Main.npcChatText = "";
			Main.inFancyUI = true;
			Main.InGameUI.SetState(Main.ManageControlsMenu);
		}

		// Token: 0x06000B3E RID: 2878 RVA: 0x0000BDDA File Offset: 0x00009FDA
		public static bool CanShowVirtualKeyboard(int context)
		{
			return UIVirtualKeyboard.CanDisplay(context);
		}

		// Token: 0x06000B3F RID: 2879 RVA: 0x003EABD0 File Offset: 0x003E8DD0
		public static void OpenVirtualKeyboard(int keyboardContext)
		{
			IngameFancyUI.CoverNextFrame();
			Main.PlaySound(12, -1, -1, 1, 1f, 0f);
			string labelText = "";
			if (keyboardContext != 1)
			{
				if (keyboardContext == 2)
				{
					labelText = Language.GetTextValue("UI.EnterNewName");
					Player player = Main.player[Main.myPlayer];
					Chest chest = Main.chest[player.chest];
					Main.npcChatText = chest.name;
					if (Main.tile[player.chestX, player.chestY].type == 21)
					{
						Main.defaultChestName = Lang.chestType[(int)(Main.tile[player.chestX, player.chestY].frameX / 36)].Value;
					}
					if (Main.tile[player.chestX, player.chestY].type == 467)
					{
						Main.defaultChestName = Lang.chestType2[(int)(Main.tile[player.chestX, player.chestY].frameX / 36)].Value;
					}
					if (Main.tile[player.chestX, player.chestY].type == 88)
					{
						Main.defaultChestName = Lang.dresserType[(int)(Main.tile[player.chestX, player.chestY].frameX / 54)].Value;
					}
					if (TileID.Sets.BasicChest[(int)Main.tile[player.chestX, player.chestY].type])
					{
						Main.defaultChestName = TileLoader.ModChestName((int)Main.tile[player.chestX, player.chestY].type);
					}
					if (TileLoader.IsDresser((int)Main.tile[player.chestX, player.chestY].type))
					{
						Main.defaultChestName = TileLoader.ModDresserName((int)Main.tile[player.chestX, player.chestY].type);
					}
					if (Main.npcChatText == "")
					{
						Main.npcChatText = Main.defaultChestName;
					}
					Main.editChest = true;
				}
			}
			else
			{
				Main.editSign = true;
				labelText = Language.GetTextValue("UI.EnterMessage");
			}
			Main.clrInput();
			if (!IngameFancyUI.CanShowVirtualKeyboard(keyboardContext))
			{
				return;
			}
			Main.inFancyUI = true;
			if (keyboardContext != 1)
			{
				if (keyboardContext == 2)
				{
					Main.InGameUI.SetState(new UIVirtualKeyboard(labelText, Main.npcChatText, delegate(string s)
					{
						ChestUI.RenameChestSubmit(Main.player[Main.myPlayer]);
						IngameFancyUI.Close();
					}, delegate
					{
						ChestUI.RenameChestCancel();
						IngameFancyUI.Close();
					}, keyboardContext, false));
				}
			}
			else
			{
				Main.InGameUI.SetState(new UIVirtualKeyboard(labelText, Main.npcChatText, delegate(string s)
				{
					Main.SubmitSignText();
					IngameFancyUI.Close();
				}, delegate
				{
					Main.InputTextSignCancel();
					IngameFancyUI.Close();
				}, keyboardContext, false));
			}
			UILinkPointNavigator.GoToDefaultPage(1);
		}

		// Token: 0x06000B40 RID: 2880 RVA: 0x003EAEBC File Offset: 0x003E90BC
		public static void Close()
		{
			Main.inFancyUI = false;
			Main.PlaySound(11, -1, -1, 1, 1f, 0f);
			if (!Main.gameMenu && (!(Main.InGameUI.CurrentState is UIVirtualKeyboard) || UIVirtualKeyboard.KeyboardContext == 2))
			{
				Main.playerInventory = true;
			}
			Main.InGameUI.SetState(null);
			UILinkPointNavigator.Shortcuts.FANCYUI_SPECIAL_INSTRUCTIONS = 0;
		}

		// Token: 0x06000B41 RID: 2881 RVA: 0x003EAF1C File Offset: 0x003E911C
		public static bool Draw(SpriteBatch spriteBatch, GameTime gameTime)
		{
			if (!Main.gameMenu && Main.player[Main.myPlayer].dead && !Main.player[Main.myPlayer].ghost)
			{
				IngameFancyUI.Close();
				Main.playerInventory = false;
				return false;
			}
			bool result = false;
			if (Main.InGameUI.CurrentState is UIVirtualKeyboard && UIVirtualKeyboard.KeyboardContext > 0)
			{
				if (!Main.inFancyUI)
				{
					Main.InGameUI.SetState(null);
				}
				if (Main.screenWidth >= 1705 || !PlayerInput.UsingGamepad)
				{
					result = true;
				}
			}
			if (!Main.gameMenu)
			{
				Main.mouseText = false;
				if (Main.InGameUI != null && Main.InGameUI.IsElementUnderMouse())
				{
					Main.player[Main.myPlayer].mouseInterface = true;
				}
				Main.instance.GUIBarsDraw();
				if (Main.InGameUI.CurrentState is UIVirtualKeyboard && UIVirtualKeyboard.KeyboardContext > 0)
				{
					Main.instance.GUIChatDraw();
				}
				if (!Main.inFancyUI)
				{
					Main.InGameUI.SetState(null);
				}
				Main.instance.DrawMouseOver();
				Vector2 bonus = Main.DrawThickCursor(false);
				Main.DrawCursor(bonus, false);
			}
			return result;
		}

		// Token: 0x06000B42 RID: 2882 RVA: 0x0000BDE2 File Offset: 0x00009FE2
		public static void MouseOver()
		{
			if (!Main.inFancyUI)
			{
				return;
			}
			if (Main.InGameUI.IsElementUnderMouse())
			{
				Main.mouseText = true;
			}
		}

		// Token: 0x04000F75 RID: 3957
		private static bool CoverForOneUIFrame;
	}
}
